import { _decorator, Component, } from 'cc';
import DataManager from '../Runtime/DataManager';
import { advertisingPermission, createAd } from "../api/ad"
import { userStore } from '../service/user';
const { ccclass } = _decorator;
const wx = window['wx']
@ccclass('AdManager')
export class AdManager extends Component {
    init() {
        if (typeof (wx) !== "undefined") {
            if (!DataManager.Instance.videoAd) {
                
                DataManager.Instance.videoAd = wx.createRewardedVideoAd({
                    adUnitId: 'adunit-904fdeaf67301f5c'
                })
                console.log(1111, DataManager.Instance.videoAd)
                DataManager.Instance.videoAd.onError(err => {
                    this.setAdInfo(2, err)
                    console.error('激励视频 广告显示失败', err?.errMsg)
                })
            }
        }
    }
    playAd() {
        return new Promise<Boolean>((resolve, reject) => {
            let that = this
            if (typeof (wx) !== "undefined") {
                advertisingPermission().then(async res => {
                    if (res) {
                        wx.showModal({
                            title: '提示',
                            content: '需要播放一段广告,点击确定继续',
                            async success(res) {
                                if (res.confirm) {
                                    let ad = await that.showAd()
                                    resolve(ad)
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                    resolve(false)
                                }
                            }
                        })
                    } else {
                        resolve(true)
                    }
                })
            } else {
                resolve(true)
            }
        })
    }
    showAd() {
        return new Promise<Boolean>((resolve, reject) => {
            // 用户触发广告后，显示激励视频广告
            DataManager.Instance.videoAd.show().catch(() => {
                // 失败重试
                DataManager.Instance.videoAd.load()
                    .then(() => DataManager.Instance.videoAd.show())
                    .catch(err => {
                        resolve(true)
                        this.setAdInfo(2, err)
                        console.error('激励视频 广告显示失败', err?.errMsg)
                    })
            })
            DataManager.Instance.videoAd.onClose(res => {
                // 用户点击了【关闭广告】按钮
                // 小于 2.1.0 的基础库版本，res 是一个 undefined
                if (res && res.isEnded || res === undefined) {
                    // 正常播放结束，可以下发游戏奖励
                    console.log("正常播放结束")
                    resolve(true)
                    this.setAdInfo(1, '正常播放结束')
                }
                else {
                    // 播放中途退出，不下发游戏奖励
                    resolve(false)
                    this.setAdInfo(0, '播放中途退出')
                    console.log("播放中途退出，不下发游戏奖励")
                }
            })
        })

    }
    setAdInfo(status: any, remark = '') {
        createAd({ status, remark }).then(res => {
            if (status == 1) {
                userStore().get();
            }
        })
    }
}


